package org.springblade.common.utils;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import liquibase.pro.packaged.E;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.launch.constant.TokenConstant;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.modules.repair.entity.MaintainWxmsgType;
import org.springblade.modules.repair.service.IMaintainMalfunctionWorkService;
import org.springblade.modules.repair.service.IMaintainWxmsgTypeService;
import org.springblade.modules.repair.utils.MapUtil;
import org.springblade.modules.repair.vo.plan.wx.WxUserPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Date 9:13 2023/4/18
 * @Description {
 * HTTP 请求工具类 异步
 * }
 * @Author lsay
 **/
@Component
@Slf4j
public class AsyncHttpUtil {

	/**
	 * @return 请求结果
	 * @Author lsay
	 * @Description {
	 * POST 请求
	 * }
	 * @Date 9:16 2023/4/18
	 * @Param url 请求地址
	 * @Param body 请求参数
	 **/
	@Async
	public void createPost(String url, String body) {
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}", "POST", url, body);
		String res = HttpUtil.createPost(url).body(body).execute().body();
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}, 结果:{}", "POST", url, body, res);
	}

	/**
	 * POST请求 (携带token)
	 *
	 * @param url
	 * @param body
	 * @param request
	 */
	@Async
	public void createPost(String url, String body, HttpServletRequest request) {
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}, 请求token:{}", "POST", url, body, AuthUtil.getHeader(request));
		String res = HttpUtil.createPost(url).header(TokenConstant.HEADER, AuthUtil.getHeader(request)).body(body).execute().body();
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求体:{}, 结果:{}", "POST", url, body, res);
	}

	/**
	 * GET请求 (携带token)
	 */
	@Async
	public void createGet(String url, HttpServletRequest request) {
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 请求token:{}", "GET", url, request);
		String res = HttpUtil.createGet(url).header(TokenConstant.HEADER, AuthUtil.getHeader(request)).execute().body();
		log.info("调用外部接口,请求方法:{}, 请求地址:{}, 结果:{}", "GET", url, res);
	}

}
